<?php

namespace app\common\model\logic;

class AuthApisBindModel
{
    public function __construct()
    {
        $this->curModel = model('dao.auth_apis_bind');
    }

    public function getMenuPower($data)
    {
        $where[] = ['target_type', '=', 1];
        $where[] = ['target_id', '=', $data['id']];
        $apisList = $this->getPower($where);
        if (empty($apisList)) {
            exception(lang('AUTH_NOT'), 0, 'app\\common\\exception\\LogicException');
        } else {
            return $apisList[0];
        }
    }

    public function saveMenuPower($data)
    {
        $data['target_type'] = 1;
        $data['target_id'] = $data['id'];

        return $this->savePower($data);
    }

    public function getApisAll($data)
    {
        $where[] = ['target_type', '=', $data['target_type']];
        $where[] = ['target_id', 'in', $data['id_list']];

        return $this->curModel->where($where)->all();
    }

    public function getGroupPower($data)
    {
        $where[] = ['target_type', '=', 2];
        $where[] = ['target_id', '=', $data['id']];
        $apisList = $this->getPower($where);
        if (empty($apisList)) {
            exception(lang('AUTH_NOT'), 0, 'app\\common\\exception\\LogicException');
        } else {
            return $apisList;
        }
    }

    public function saveGroupPower($data)
    {
        $data['target_type'] = 2;
        $data['target_id'] = $data['id'];

        return $this->savePower($data);
    }

    public function getRolesPower($data)
    {
        $where[] = ['target_type', '=', 3];
        $where[] = ['target_id', '=', $data['id']];
        $apisList = $this->getPower($where);
        if (empty($apisList)) {
            exception(lang('AUTH_NOT'), 0, 'app\\common\\exception\\LogicException');
        } else {
            return $apisList;
        }
    }

    public function saveRolesPower($data)
    {
        $data['target_type'] = 3;
        $data['target_id'] = $data['id'];

        return $this->savePower($data);
    }

    private function savePower($data)
    {
        $this->clearExpBind($data);
        $insertData = [];
        $apis = model('server.auth_apis');
        $apisList = explode(',', $data['apis_list']);
        foreach ($apisList as $ve) {
            $insertRow['target_type'] = $data['target_type'];
            $insertRow['target_id'] = $data['target_id'];
            $insertRow['apis_id'] = $ve;
            $apisRow = $apis->getRow(['id' => $ve]);
            $insertRow['apis_code'] = $apisRow['code'];
            $insertRow['status'] = 1;
            $insertData[] = $insertRow;
        }
        $result = $this->curModel->saveAll($insertData);
        if ($result !== false) {
            return $result;
        } else {
            exception(lang('AUTH_ERROR'), 0, 'app\\common\\exception\\LogicException');
        }
    }

    //清理过期绑定
    private function clearExpBind($data)
    {
        $where[] = ['target_type', '=', 1];
        $where[] = ['target_id', '=', $data['target_id']];
        $bindList = $this->getBindId($where);
        if (!empty($bindList)) {
            $this->curModel->destroy($bindList); //保存前先清理以前的授权信息，避免重复授权
        }
    }

    private function getBindId($where)
    {
        $apiList = $this->curModel->where($where)->all();
        if ($apiList->isEmpty()) {
            return [];
        } else {
            $idList = [];
            foreach ($apiList as $ve) {
                $idList[] = $ve['id'];
            }

            return $idList;
        }
    }

    public function getTargetId($where)
    {
        $apiList = $this->curModel->where($where)->all();
        if ($apiList->isEmpty()) {
            return [];
        } else {
            $idList = [];
            foreach ($apiList as $ve) {
                $idList[] = $ve['target_id'];
            }

            return $idList;
        }
    }

    private function getPower($where)
    {
        $apiList = $this->curModel->where($where)->all();
        if ($apiList->isEmpty()) {
            return [];
        } else {
            $idList = [];
            foreach ($apiList as $ve) {
                $idList[] = $ve['apis_id'];
            }

            return $idList;
        }
    }
}
